package com.eric.test.list;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: ListOperationTest
 * @Date: 2024/7/1 10:43
 * @Author: LiuBing
 */
public class ListOperation {
    
    /**
     * 根据某个字段去重
     */

    public static void removeTheSame() {
        List<ListEntity> lists = ListGenerator.getObjectList();

        List<ListEntity> disUsers = lists.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ListEntity::getKey))),
                        ArrayList::new
                )
        );
        System.out.println("根据对象中的某个字段进行去重操作" + disUsers);


        Boolean flag = lists.stream().map(ListEntity::getValue).distinct().count() == lists.size();
        System.out.println(flag);
    }


    /**
     * stream 筛选出不为 null 的
     */
    public static void removeNull() {
        List<Object> collect = ListGenerator.getObjectList().stream()
                .map(listEntity -> {
                    if (listEntity.getId() == 1L) {
                        return "123";
                    }
                    return null;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        System.out.println(collect);
    }

}
